let ipts = document.querySelectorAll("#reg-content input");
let span = document.querySelector("span");
let isUsername = false;
let isPassword = false;
//获取焦点
ipts[0].onfocus= function() {
	span.innerHTML = "6-18，数字 字母 下线 ，字母开头";
	span.style.color = "gray";
};
ipts[0].onblur = function() {
	//失去焦点
	let reg = /^[a-z]\w{5,17}$/i;
	if (this.value.length < 6) {
		span.innerHTML = "长度不够6位";
		span.style.color = "red";
		isUsername = false;
	} else {
		if (reg.test(this.value)) {
			axios.get(checkusernameAPI, {
				//发送请求
				params: {
					username: ipts[0].value
				},
			}).then(function(res) {
				//请求数据响应
				let obj = res.data;
				span.innerHTML = obj.msg;
				if (obj.code == 1) {
					span.style.color = "green";
					isUsername = true;
				} else {
					span.style.color = "red";
					isUsername = false;
				}
			});
		} else {
			// 如果不满足正则 提示 只能 只能输入字母、数字、下划线
			span.innerHTML = "只能输入字母、数字、下划线";
			span.style.color = "red";
			isUsername = false;
		}
	}
};
//获取焦点
ipts[1].onfocus = function() {
	this.nextElementSibling.innerHTML = "6-16个字符，区分大写";
}
ipts[1].onblur = function() {
	let reg = /^\S{6,16}$/;
	if (reg.test(this.value)) {
		this.nextElementSibling.innerHTML = "";
		isPassword = true;
	} else {
		this.nextElementSibling.innerHTML =
			"格式有误，必须是6-16个字符，区分大写";
		isPassword = false;
	}
}

function regClick() {
	//判断输入
	if (isPassword && isUsername) {
		//如果条件满租发起请求
		let username = ipts[0].value;
		let password = ipts[1].value;
		axios.get(regAPI, {
			params: {
				username,
				password
			}
		}).then(function(res) {
			let obj1 = res.data;
			alert(obj1.msg);
			if (obj1.code == 1) {
				location.href = "login.html";
			}
		});
	}
}
